#include "../server.hpp"
#include <iostream>
#include <chrono>
#include <thread>
#include <cassert>

void logMessage(const std::string& message) {
    std::cout << message << std::endl;
}

int main() {
    try {
        Socket cli_sock;
        if (!cli_sock.CreateClient(8085, "127.0.0.1")) {
            throw std::runtime_error("Failed to create client socket");
        }

        // 请求头，声明内容长度为100字节
        std::string req = "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 100\r\n\r\n";
        std::string body = "bitejiuyeke"; // 实际正文只有11字节

        for (int i = 0; i < 3; ++i) { // 测试3次
            // 发送请求头
            if (cli_sock.Send(req.c_str(), req.size()) == -1) {
                throw std::runtime_error("Failed to send request header");
            }
            // 发送正文
            if (cli_sock.Send(body.c_str(), body.size()) == -1) {
                throw std::runtime_error("Failed to send request body");
            }

            char buf[1024] = {0};
            if (!cli_sock.Recv(buf, 1023)) {
                throw std::runtime_error("Failed to receive response");
            }

            logMessage("Received: " + std::string(buf));
            std::this_thread::sleep_for(std::chrono::seconds(3)); // 等待3秒
        }

        cli_sock.Close();
    } catch (const std::exception& e) {
        logMessage("Error: " + std::string(e.what()));
        return 1;
    }

    return 0;
}
